/*
  登录许可权限
*/
import router from '@/router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'// progress bar style
import {isAuthenticated, getCurrentUser} from '@/utils/common' // getToken from cookie

import {pathName} from './setBaseUrl'

// 判断cn/us
const isChina = true

NProgress.configure({showSpinner: false})// NProgress Configuration

router.beforeEach(function (to, from, next) {
    NProgress.start() // start progress bar
    if (to.matched.some(record => record.meta.noRequireAuth)) {
        // 路由元信息 noRequireAuth: true则不做登录校验
        next()
    } else {
        if (isAuthenticated()) { // 判断用户
            if (to.path === '/login') {
                next({path: '/'})
                NProgress.done()
            } else {
                if (to.meta.init) {
                    let iconSrc = !isChina ? '/favicon.ico' : '/favicon.plg.ico'
                    let title = !isChina ? 'CAD - 幼儿评估发展系统' : 'CAD - 幼儿评估发展系统'
                    document.getElementById('faviconIcon').setAttribute('href', iconSrc)
                    document.title = title
                }
                if (to.meta.title) {
                    // 如果设置标题，拦截后设置标题
                    document.title = to.meta.title
                }
                next()
            }
        } else {
            // 重定向到登录页面,登录页面会根据当前的url来重定向到登录后的页面
            // next({name: 'login', query: {redirect: to.fullPath}})
            // NProgress.done()
        }
    }
})

router.afterEach(() => {
    NProgress.done() // finish progress bar
})
